package com.pinyougou.shop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.pojo.TbGoods;
import com.pinyougou.sellergoods.service.GoodsService;
import entity.Goods;
import entity.PageResult;
import entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * controller
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

	@Reference
	private GoodsService goodsService;


	/**
	 * 返回全部列表
	 *
	 * @return
	 */
	@RequestMapping("/findAll")
	public List<TbGoods> findAll() {
		return goodsService.findAll();
	}


	/**
	 * 返回全部列表
	 *
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult findPage(int page, int rows) {
		return goodsService.findPage(page, rows);
	}

	/**
	 * 增加
	 *
	 * @param goods
	 * @return
	 */
	@RequestMapping("/add")
	public Result add(@RequestBody Goods goods) {
//		判断非法操作
		TbGoods tbGoods = goods.getTbGoods();
		Long goodsId = tbGoods.getId();
		String name = SecurityContextHolder.getContext().getAuthentication().getName();
		if (goodsId != null) {
			TbGoods tbGoods2 = findOneByOne(goodsId);
			String sellerId = tbGoods2.getSellerId() + "";
			if (!name.equals(sellerId) || !sellerId.equals(goods.getTbGoods().getSellerId())) {
				return new Result(false, "非法操作");
			}
		}
//		判断结束
		tbGoods.setSellerId(name);
		try {
			goodsService.update(goods);
			return new Result(true, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "失败");
		}
	}

	/**
	 * 修改
	 *
	 * @param goods
	 * @return
	 */
	@RequestMapping("/update")
	public Result update(@RequestBody Goods goods) {
		try {
			goodsService.update(goods);
			return new Result(true, "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "修改失败");
		}
	}

	@RequestMapping("findOneByOne")
	public TbGoods findOneByOne(Long id) {
		TbGoods one = goodsService.findOneByOne(id);
		return one;
	}

	/**
	 * 获取实体
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping("/findOne")
	public Goods findOne(Long id) {

		Goods one = goodsService.findOne(id);
		return one;
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public Result delete(Long[] ids) {
		try {
			goodsService.delete(ids);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}

	/**
	 * 查询+分页
	 *
	 * @param
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/search")
	public PageResult search(@RequestBody TbGoods goods, int page, int rows) {
		String name = SecurityContextHolder.getContext().getAuthentication().getName();
		goods.setSellerId(name);
		return goodsService.findToPage(goods, page, rows);
	}

	@RequestMapping("/isMarketable")
	public Result isMarketable(String number, Long goodsId) {
		try {
			goodsService.isMarketable(number, goodsId);
			return new Result(true, "上架成功");
		} catch (Exception e) {
			return new Result(false, "上架失败");
		}
	}
}
